package org.example.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.base.annotation.ExamplePermission;
import org.example.base.util.AjaxResult;
import org.example.base.util.PageInfo;
import org.example.org.domain.Department;
import org.example.org.domain.Employee;
import org.example.org.query.DepartmentQuery;
import org.example.org.query.EmployeeQuery;
import org.example.org.service.IDepartmentService;
import org.example.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
@ExamplePermission(name = "员工管理",desc="员工管理的CRUD")
@Api(value = "员工管理",description = "员工操作的CRUD")
@RequestMapping("/employee")
@RestController//该controller接口中均返回数据

public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    //查一个 get
    @GetMapping("/{id}")
    @ExamplePermission(name = "员工查询",desc="根据员工id查询指定员工")
    public AjaxResult selectByID(@PathVariable("id")Long id){
        try {
            Employee employee = service.selectById(id);
            return AjaxResult.success(employee);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //更新或新增
    @ApiOperation(value="更新或新增")
    @PutMapping
    @ExamplePermission(name = "员工的保存",desc="员工的新增和修改")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if(Objects.isNull(employee.getId())){
                service.insert(employee);
            }else{
                service.update(employee);
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    //删delete
    @DeleteMapping("/{id}")
    @ExamplePermission(name = "员工的删除",desc="根据id删除指定员工")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.success();

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    //查全部 get
    @GetMapping
    @ExamplePermission(name = "员工查询",desc="查询所有员工")
    public AjaxResult selectAll(){
        try {
            List<Employee> departments = service.selectAll();
            return AjaxResult.success(departments);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }




    @PostMapping
    @ExamplePermission(name = "分页查询",desc="员工的分页查询")
    public AjaxResult queryPage(@RequestBody EmployeeQuery empoyeeQuery){
        try {
            PageInfo pageInfo=service.queryPage(empoyeeQuery);
            return AjaxResult.success(pageInfo);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    @ExamplePermission(name = "员工删除",desc="员工的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}